/* Write a stream based program which will accept Roll Number, Name, Age and
 * Address from user. Age and Roll-no should be numeric.  Handle with built-in
 * exception. None of the field should be blank. Handle with custom exception,
 * Ask user ,whether to write the data in the file. If answer is yes then data
 * is saved into a file as an object(User can write many records in the file),
 * otherwise terminate the current program
 * 
 * Write another program to display all the records saved into the file
 */

package day4.p2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;

public class StudentDatabase {
	public static void main(String[] args) throws IOException,
			ClassNotFoundException {
		/*
		 * String ch = "y"; Scanner sc = new Scanner(System.in);
		 * ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
		 * "Student.txt", true)); while (ch.charAt(0) == 'y' || ch.charAt(0) ==
		 * 'Y') { Student obj = new Student(); obj.create();
		 * oos.writeObject(obj); System.out.print("Add Another student(y/n): ");
		 * ch = sc.next(); } oos.flush(); oos.close();
		 * 
		 * System.out.print("Display Full database(y/n): "); ch = sc.next(); if
		 * (ch.charAt(0) == 'n' || ch.charAt(0) == 'N') System.exit(0);
		 * 
		 * ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
		 * "Student.dat")); System.out.println("RollNo\tName\tAge\tAddress");
		 * Object obj; try { while (true) { obj = ois.readObject(); Student s =
		 * (Student) obj; System.out.println(s); } } catch (EOFException e) {
		 * System.out.println("EndOfFile reached."); } catch
		 * (StreamCorruptedException e) { System.out.println("File Corrupted.");
		 * } finally { ois.close(); }
		 *//*
			 * try { while (true) System.out.println((Student)
			 * ois.readObject()); } catch (StreamCorruptedException e) {
			 * System.out.println("File Corrupted."); } catch (EOFException e) {
			 * System.out.println("EndOfFile reached."); } finally {
			 * ois.close(); }
			 */
		/*
		 * Scanner sc = new Scanner(System.in);
		 * System.out.print("No of entries: "); int count = sc.nextInt(); int i
		 * = count * 2;
		 * 
		 * ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
		 * "Student.txt", true)); Student st; while (count-- > 0) { st = new
		 * Student(); st.create(); oos.writeObject(st); } oos.close();
		 * FileInputStream fis = new FileInputStream("Student.txt");
		 * ObjectInputStream ois = new ObjectInputStream(fis); Object obj; try {
		 * while (i-- > 0) { ois.reset(); obj = ois.readObject(); Student ee =
		 * (Student) obj; System.out.println(ee); } } catch (EOFException eee) {
		 * } ois.close();
		 */

		File file = new File("student.dat");
		ObjectOutputStream out = null;

		Student sObj;
		Scanner sc = new Scanner(System.in);

		System.out.print("Add Student info:");
		String ch = sc.next();
		// try{
		if (file.exists())
			out = new AppendableObjectOutputStream(new FileOutputStream(file,
					true));
		else
			out = new ObjectOutputStream(new FileOutputStream(file));

		while (ch.charAt(0) == 'y' || ch.charAt(0) == 'Y') {
			sObj = new Student();
			sObj.create();
			out.writeObject(sObj);
			out.flush();
			sObj = null;
			System.out.print("Add anoter Student info (y/n): ");
			ch = sc.next();
		}

		// }catch(){}

	}
}
